package com.soft.action.business;

import com.soft.model.Business;
import com.soft.model.Sales;
import com.soft.model.User;
import com.soft.service.AddressService;
import com.soft.service.GarbageTypeService;
import com.soft.service.SalesService;
import com.soft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商户管理回收员控制器
 */
@Controller
@RequestMapping("/business/salesManager")
public class SalesManagerAction {

    @Autowired
    private SalesService userService;

    // 跳转到会员管理界面
    @RequestMapping("/toUserManager")
    public String toUserManager(Map<String, Object> map, HttpSession session) {

        Business business = (Business)session.getAttribute("admin");

        List<Sales> users = userService.getAll(business.getId());
        map.put("users", users);
        return "business/salePersonManager";

    }

    // 查询
    @RequestMapping("/search")
    public String search(String username, Map<String, Object> map, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();

        if (username != null && !"".equals(username)) {
            Sales user = userService.getByUserName(username);
            if (user == null) {
                System.out.println("用户不存在！");
                out.write(
                        "<script>alert('回收员不存在！');" + "window.location.href='/garbage-collection/salesManager/toUserManager'</script>");
            } else {
                List<Sales> users = new ArrayList<Sales>();
                users.add(user);
                map.put("users", users);
                return "business/salePersonManager";
            }

        } else {
            return "redirect:toUserManager";
        }
        return "redirect:toUserManager";
    }


    // 修改
    @RequestMapping("/update")
    public String update(User user) {
        if (user != null) {
            // 这个是更新入库的user
            Sales newUser = userService.getById(user.getId());

            if (user.getUsername() != null && !"".equals(user.getUsername())) {
                newUser.setUsername(user.getUsername());
                System.out.println("修改了用户名");
            }
            if (user.getPassword() != null && !"".equals(user.getPassword())) {
                newUser.setPassword(user.getPassword());
                System.out.println("修改了密码");
            }
            if (user.getMoney() != null) {
                //newUser.setMoney(user.getMoney());
                System.out.println("修改了积分");
            }
            userService.update(newUser);
        }
        return "redirect:toUserManager";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        userService.delete(id);
        return "redirect:toUserManager";
    }
}
